﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using DocumentFormat.OpenXml.Lite.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Core.Interfaces;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table
{
    [XmlRoot("tbl", Namespace = Constants.WordMain)]
    public class Table : XmlSerializable, IOfficeElement
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        [XmlElement("tblPr", Namespace = Constants.WordMain)]
        public TableProperties Properties { get; set; }

        [XmlElement("tblGrid", Namespace = Constants.WordMain)]
        public TableGrid Grid {
            get { return grid; }
            set { grid = value; grid.DocumentId = this.DocumentId; }
        }

        [Optional]
        [XmlElement("tr", Namespace = Constants.WordMain)]
        public XmlList<TableRow> Rows { get; set; }

        [XmlIgnore]
        private TableGrid grid;

        [XmlIgnore]
        public TableRow this[int i]
        {
            get { return Rows[i]; }
            set { Rows[i] = value; }
        }

        public Table()
        {
            Rows = new XmlList<TableRow>();
            Rows.Changing += Rows_Changing;

            Properties = new TableProperties();
        }

        public Table(int columns, int rows)
            : this()
        {
            for (int i = 0; i < rows; ++i)
            {
                Rows.Add(new TableRow(columns));
            }
        }

        public TableRow AddRow()
        {
            TableRow r = new TableRow();
            r.DocumentId = this.DocumentId;
            Rows.Add(r);
            return r;
        }
        
        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddProperty(ITablePropElement element)
        {
            var obj = Properties.Properties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITablePropElement).Copy(element);
            }
            else
            {
                Properties.Properties.Add(element);
                return true;
            }

            return false;
        }

        #region Events

        void Rows_Changing(object sender, ListEventArgs<TableRow> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                obj.DocumentId = this.DocumentId;
            }
        }

        #endregion
    }
}
